var t = getApp();

Page({
    data: {
        isShowStreet: !1,
        form: {
            username: "",
            mobile: "",
            province: "",
            city: "",
            district: "",
            streetName: "",
            address: "",
            isDefault: !1
        },
        addressName: "",
        openDateLw: !1,
        provinceData: [],
        cityData: [],
        districtData: [],
        pickerValue: [ 0, 0, 0 ],
        selectAddressData: [],
        districtVal: [],
        opren: !0,
        acr: !0,
        is_region: 0
    },
    onLoad: function(e) {
        if (e.model) {
            var a = JSON.parse(e.model);
            this.setData({
                "form.isDefault": 0 != a.isdefault,
                "form.province": a.province,
                "form.address_id": a.id,
                "form.username": a.username,
                "form.mobile": a.mobile,
                "form.city": a.city,
                "form.district": a.district,
                "form.address": a.address,
                "form.streetName": a.street,
                addressName: a.province + " " + a.city + " " + a.district,
                isShowStreet: !t._isTextEmpty(a.street),
                model: a
            }), this.data.isShowStreet && this._getStreet(a.district_id);
        }
        this._initAddressInfo(), this.getIsOpenAddress();
    },
    streetClose: function(t) {
        this.setData({
            streetShow: !1,
            opren: !0
        });
    },
    onReady: function() {},
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {},
    getIsOpenAddress: function() {
        var e = this, a = t.getNetAddresss("member.member-address.is-region");
        t._getNetWork({
            url: a,
            success: function(a) {
                var s = a.data;
                1 != s.result || t._isTextEmpty(s.data) ? console.log(s.msg) : e.setData({
                    is_region: s.data.is_region
                });
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    _getStreet: function(e) {
        var a = this, s = t.getNetAddresss("member.memberAddress.street");
        s += "&district_id=" + e, t._getNetWork({
            url: s,
            success: function(e) {
                var s = e.data;
                1 != s.result || t._isTextEmpty(s.data) ? a.setData({
                    isShowStreet: !1
                }) : !t._isTextEmpty(s.data) && s.data.length > 0 ? a.setData({
                    districtVal: s.data,
                    isShowStreet: !0
                }) : a.setData({
                    isShowStreet: !1
                });
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    isDefaultChange: function(t) {
        var e = t.detail;
        this.setData({
            "form.isDefault": e
        });
    },
    usernameinp: function(t) {
        var e = t.detail.value;
        this.setData({
            "form.username": e
        });
    },
    mobileinp: function(t) {
        var e = t.detail.value;
        this.setData({
            "form.mobile": e
        });
    },
    addressinp: function(t) {
        var e = t.detail.value;
        console.log(e), this.setData({
            "form.address": e
        });
    },
    _closeDateLw: function() {
        this.setData({
            openDateLw: !1
        });
    },
    _initAddressInfo: function() {
        var e = this;
        wx.getStorage({
            key: "provinceData",
            success: function(t) {
                console.log("地址信息准备就绪"), e._getProvinceData();
            },
            fail: function(a) {
                console.log("省fail"), t._getNetWork({
                    url: t.getNetAddresss("member.member-address.address"),
                    success: function(t) {
                        var a = t.data, s = a.data.province, i = a.data.city, o = a.data.district;
                        wx.setStorage({
                            key: "provinceData",
                            data: s
                        }), wx.setStorage({
                            key: "cityData",
                            data: i
                        }), wx.setStorage({
                            key: "districtData",
                            data: o
                        }), e._initAddressInfo();
                    },
                    fail: function(t) {},
                    state: !0
                });
            }
        });
    },
    _getProvinceData: function() {
        var t = this;
        wx.getStorage({
            key: "provinceData",
            success: function(e) {
                console.log("省读取成功", e.data), t.setData({
                    provinceData: e.data
                }), t.setData({
                    "selectAddressData[0].name": e.data[t.data.pickerValue[0]].areaname,
                    "selectAddressData[0].id": e.data[t.data.pickerValue[0]].id,
                    "selectAddressData[0].index": t.data.pickerValue[0]
                }), t._getCityData();
            }
        });
    },
    _getCityData: function() {
        var t = this.data.selectAddressData[0];
        console.log("城市", "_data", t);
        var e = this;
        wx.getStorage({
            key: "cityData",
            success: function(a) {
                console.log("城市读取成功", a);
                for (var s = [], i = 0; i < a.data.length; i++) a.data[i].parentid == t.id && s.push(a.data[i]);
                console.log("城市数据筛选", s), e.setData({
                    cityData: s
                }), e.setData({
                    "selectAddressData[1].name": s[e.data.pickerValue[1]].areaname,
                    "selectAddressData[1].id": s[e.data.pickerValue[1]].id,
                    "selectAddressData[1].index": e.data.pickerValue[1]
                }), e._getDistrictData();
            }
        });
    },
    _getDistrictData: function() {
        var t = this.data.selectAddressData[1];
        console.log("地区", "_data", t), console.log(t);
        var e = this;
        wx.getStorage({
            key: "districtData",
            success: function(a) {
                console.log("地区读取成功");
                for (var s = [], i = 0; i < a.data.length; i++) a.data[i].parentid == t.id && s.push(a.data[i]);
                console.log("地区数据筛选", s), e.setData({
                    districtData: s
                }), console.log("districtData", s), e.setData({
                    "selectAddressData[2].name": s[e.data.pickerValue[2]].areaname,
                    "selectAddressData[2].id": s[e.data.pickerValue[2]].id,
                    "selectAddressData[2].index": e.data.pickerValue[2]
                }), console.log("over", e.data.selectAddressData);
            }
        });
    },
    openDateLwtbtn: function() {
        this.setData({
            openDateLw: !0
        });
    },
    bindChange: function(t) {
        var e = t.detail.value;
        this.data.pickerValue[0] != e[0] && (e[1] = 0, e[2] = 0), this.data.pickerValue[1] != e[1] && (e[2] = 0), 
        this.setData({
            "pickerValue[0]": e[0],
            "pickerValue[1]": e[1],
            "pickerValue[2]": e[2]
        }), this._initAddressInfo();
    },
    _okAddress: function() {
        var t = this.data.selectAddressData;
        this.setData({
            "form.streetName": "",
            districtVal: "",
            addressName: t[0].name + " " + t[1].name + " " + t[2].name,
            "form.province": t[0].name,
            "form.city": t[1].name,
            "form.district": t[2].name
        }), this._getStreet(t[2].id), this._closeDateLw();
    },
    streetChoose: function() {
        1 == this.data.is_region && t._isTextEmpty(this.data.addressName) ? wx.showToast({
            icon: "none",
            title: "请先选择所在地区",
            duration: 1500
        }) : this.setData({
            streetShow: !0,
            opren: !1
        });
    },
    streetConfirm: function(t) {
        var e = t.target.dataset.strname;
        this.setData({
            "form.streetName": e,
            streetShow: !1,
            opren: !0
        }), console.log(this.data.form.streetName, this.data.streetShow, this.data.opren);
    },
    alterAddresshandle: function() {
        var e;
        if (t._isTextEmpty(this.data.form.username)) wx.showToast({
            icon: "none",
            title: "请输入收货人姓名",
            duration: 1500
        }); else if (t._isTextEmpty(this.data.form.mobile)) wx.showToast({
            icon: "none",
            title: "请输入手机机号",
            duration: 1500
        }); else if (t._isMoblie(this.data.form.mobile)) wx.showToast({
            icon: "none",
            title: "请输入正确的手机号",
            duration: 1500
        }); else if (1 == this.data.is_region && t._isTextEmpty(this.data.addressName)) wx.showToast({
            icon: "none",
            title: "请选择所在区域",
            duration: 1500
        }); else if (t._isTextEmpty(this.data.form.address)) wx.showToast({
            icon: "none",
            title: "请输入详细地址",
            duration: 1500
        }); else {
            var a = {};
            a = this.data.isShowStreet ? {
                address_id: this.data.form.address_id,
                username: this.data.form.username,
                mobile: this.data.form.mobile,
                city: this.data.form.city,
                address: this.data.form.address,
                province: this.data.form.province,
                isdefault: this.data.form.isDefault ? 1 : 0,
                district: this.data.form.district,
                street: this.data.form.streetName
            } : {
                address_id: this.data.form.address_id,
                username: this.data.form.username,
                mobile: this.data.form.mobile,
                city: this.data.form.city,
                address: this.data.form.address,
                province: this.data.form.province,
                isdefault: this.data.form.isDefault ? 1 : 0,
                district: this.data.form.district
            }, e = t.getNetAddresss("member.member-address.update"), t._postNetWork({
                url: e,
                showToastIn: !1,
                data: a,
                success: function(t) {
                    var e = t.data;
                    1 == e.result ? wx.showToast({
                        title: e.msg,
                        icon: "none",
                        duration: 1500,
                        success: function() {
                            setTimeout(function() {
                                wx.navigateBack({
                                    delta: 1
                                });
                            }, 1500);
                        }
                    }) : wx.showToast({
                        icon: "none",
                        title: e.msg,
                        duration: 1500
                    });
                },
                fail: function(t) {}
            });
        }
    }
});